/**
 * Copyright 2020 Inc.
 **/
package com.myz.alibaba.nacos.config.ext;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * nacos作为注册中心
 *
 * @author maoyz0621 on 20-3-29
 * @version v1.0
 */
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfigExtDiscovery {


    public static void main(String[] args) {
        SpringApplication.run(NacosConfigExtDiscovery.class, args);
    }

    // 设置@Value注解取值不到忽略（不报错）
    @Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        PropertySourcesPlaceholderConfigurer c = new PropertySourcesPlaceholderConfigurer();
        c.setIgnoreUnresolvablePlaceholders(true);
        return c;
    }

    // 动态刷新
    @RefreshScope
    @RestController
    @RequestMapping("/config")
    public class EchoController {

        @Value("${config.address}")
        private String val;

        // 此属性值多文件都存在
        @Value("${config.common}")
        private String common;

        @Value("${mysql.host}")
        private String host;

        @Value("${redis.port}")
        private int port;

        @GetMapping(value = "/get")
        public String echo() {
            return "Hello Nacos Config " + val + ",相同配置: " + common + ",middle: " + port + " ,mysql:" + host;
        }
    }
}
